﻿package com.calefk.modules.perspective 
{
	import com.calefk.core.EntitySimple;
	import com.calefk.modules.math.Vector3D;
	import flash.display.MovieClip;
	
    public class Entity3D extends EntitySimple {
        // world position
        protected var mPosition:Vector3D;
        
        public function get wposition():Vector3D { return mPosition; }
        
        public function get wx():Number { return mPosition.x; }
        public function get wy():Number { return mPosition.y; }
        public function get wz():Number { return mPosition.z; }
        
        public function set wx(value:Number):void { mPosition.x = value; }
        public function set wy(value:Number):void { mPosition.y = value; }
        public function set wz(value:Number):void { mPosition.z = value; }
        
        // references
        private var mCamera3D:Camera3D;
        private var mInflate:Number = 1;
        
        public function set inflate(value:Number) { mInflate = value; }
        
        public function Entity3D(canvas:MovieClip, nameClip:String = null , camera3D:Camera3D = null , n:Number = 0) {
            super(canvas, nameClip, -5000, -5000);
            
			mCamera3D = camera3D;
            mPosition = new Vector3D();
            
            if (mClip.txtValue) {
                mClip.txtValue.text = n;
            } 
        }
        
        public function scale(s:Number):void {
            mClip.scaleX = s * mInflate;
            mClip.scaleY = s * mInflate;
        }
        
        public function visible(value:Boolean):void {
            mClip.visible = value;
        }

        override public function update(dt:int):void {
            // render relative position to camera
            mCamera3D.render(this);
            setPositionToWorld(wx , wz + pz);

        }
        
        public function setPositionToWorld(x:Number , z:Number):void {
            if (mClip.pic) {
                mClip.pic.x = (x * 1000 / 10000) - 500;
                mClip.pic.y = (z * 1000 / 10000) - 500;
            }
        }
        
        override public function clean():void {
            mCamera3D = null;
            super.clean();
        }
        
    }

}